//
//  ParameterCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/4/8.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "ParameterCollectionViewCell.h"
#import "ParameterContentCollectionViewCell.h"
#import "ParameterModel.h"
#import "ParameterCollectionReusableView.h"

@interface ParameterCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *collectionV;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end




@implementation ParameterCollectionViewCell

-(void)dealloc
{
    [_ParameterId release];
    [_collectionV release];
    [_dataArr release];
    [_priceRange release];
    [super dealloc];
    
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self createHandle];
        [self createCollectionView];
    }
    return self;
}

#pragma mark - 位置
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    
    [super applyLayoutAttributes:layoutAttributes];
//    [self createHandle];
}

//-(void)setParameterId:(NSString *)ParameterId
//{
//    if (_ParameterId != ParameterId) {
//        _ParameterId = ParameterId;
//    }
////    [self createHandle];
//    
//}


#pragma mark - 以后再嵌套collectionView的时候数据解析在这进行
- (void)layoutSubviews{
    [super layoutSubviews];
    [self createHandle];
}


#pragma mark - 数据解析
- (void)createHandle
{
    if (![self.priceRange isEqualToString:@"暂无报价"] && ![self.priceRange isEqualToString:@"未量产发售"] && ![self.priceRange isEqualToString:@"国内未发售"]) {
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/detailComparev45?fmt=json&serialId=%@", self.ParameterId];
    
    NSLog(@"22%@", self.ParameterId);
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"detailArray"][0];
        NSArray *arr = dic[@"detail"];
        self.dataArr = [ParameterModel transformWithArray:arr];
        
        [_collectionV reloadData];
    } filed:^(NSError *error) {
        
    }];
  }
}



#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((BOUNDS_WIDTH - 2) / 2, 40);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.minimumLineSpacing = 2;
    flowLayout.headerReferenceSize = CGSizeMake(BOUNDS_WIDTH, 40);
    //flowLayout.headerReferenceSize = CGSizeMake(BOUNDS_WIDTH, 40);
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
    self.collectionV.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.contentView addSubview:self.collectionV];
    
    
    [self.collectionV registerClass:[ParameterContentCollectionViewCell class] forCellWithReuseIdentifier:@"ParameterContentCollectionViewCell"];
    
    // collectionView 的section 的重用注册池
    [self.collectionV registerClass:[ParameterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ParameterCollectionReusableView"];
    
    
    [flowLayout release];
    [_collectionV release];
}

#pragma mark - collectionView的两个协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.dataArr[section] groupDetail] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ParameterContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ParameterContentCollectionViewCell" forIndexPath:indexPath];
    ParameterModel *PModel = self.dataArr[indexPath.section];
    cell.lab.text = PModel.groupDetail[indexPath.row];
    
    return cell;    
}

#pragma mark - 设置collectionView的section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArr.count;
}

#pragma mark - 设置collectionView的分区头文字
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    ParameterCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ParameterCollectionReusableView" forIndexPath:indexPath];
    
    reusableView.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    
    ParameterModel *pModel = self.dataArr[indexPath.section];
    reusableView.sectionLab.text = pModel.groupName;
    
    
    return reusableView;
}



@end
